package com.lanlian.cayman.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.RiskStateActivity;
import com.lanlian.cayman.utils.GridviewHelp;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.uikit.MyGridView;
import com.lanlian.uikit.ResearchSelectAdapter;
import com.lanlian.utillib.MaxTextLengthFilter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/13 15:49
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
@SuppressLint("ValidFragment")
public class RiskResearchOneFragment extends BaseFragment implements View.OnClickListener {

    private GridView mGridView;
    private TextView mTvNext;
    private ResearchSelectAdapter mAdapter1;

    private GridView mGridView2;
    private ResearchSelectAdapter mAdapter2;

    private ResearchSelectAdapter mAdapter4;
    private ResearchSelectAdapter mAdapter5;
    private OnNextClickListener mOnNextClickListener;

    private CheckedTextView mTvItem1;
    private CheckedTextView mTvItem2;
    private CheckedTextView mTvItem3;
    private CheckedTextView mTvItem4;

    private EditText mEt;
    private MyGridView mGridView4;
    private MyGridView mGridView5;
    /**
     * 问题
     */
    ArrayList<String> quest = new ArrayList<>();

    /**
     * 当前选择的项
     */
    int cintex = -1;

    GridviewHelp gh = new GridviewHelp();

    public interface OnNextClickListener {
        void onNextClick();
    }

    public RiskResearchOneFragment(OnNextClickListener onNextClickListener) {
        this.mOnNextClickListener = onNextClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_research_one, null);
        initView(view);
        initData();
        setListeners();
        setAdapter();
        return view;
    }

    @Override
    protected void initData() {
        quest = new ArrayList<>();
        quest.add("1、投资目的（可多选）");
        quest.add("2、主要投资目的");
        quest.add("3、预投金额（RMB）");
        quest.add("4、首选投资类型");
        quest.add("5、投资期限");
        quest.add("6、您认为需要多长时间才能达到您预期的投资回报？");
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mGridView = view.findViewById(R.id.grid_view);
        mGridView2 = view.findViewById(R.id.grid_view2);
        mEt = view.findViewById(R.id.et);
        mGridView4 = view.findViewById(R.id.grid_view4);
        mGridView5 = view.findViewById(R.id.grid_view5);
        mTvNext = view.findViewById(R.id.tv_next);

        CheckedTextView item1 = view.findViewById(R.id.tv_item2_1);
        CheckedTextView item2 = view.findViewById(R.id.tv_item2_2);
        CheckedTextView item3 = view.findViewById(R.id.tv_item2_3);
        CheckedTextView item4 = view.findViewById(R.id.tv_item2_4);
        List<CheckedTextView> list = new ArrayList<>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        gh.setList(list);


        mTvItem1 = view.findViewById(R.id.tv_item1);
        mTvItem2 = view.findViewById(R.id.tv_item2);
        mTvItem3 = view.findViewById(R.id.tv_item3);
        mTvItem4 = view.findViewById(R.id.tv_item4);
        setListener(mTvItem1);
        setListener(mTvItem2);
        setListener(mTvItem3);
        setListener(mTvItem4);

        mEt.setFilters(new InputFilter[]{new MaxTextLengthFilter(150, getActivity())});
    }

    private void setListener(CheckedTextView ctv) {
        ctv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTvItem1 != ctv) {
                    mTvItem1.setChecked(false);

                } else {
                    cintex = 0;
                    mEt.setText("");
                    mEt.setFocusableInTouchMode(false);
                    mEt.setFocusable(false);
                }
                if (mTvItem2 != ctv) {
                    mTvItem2.setChecked(false);

                } else {
                    cintex = 1;
                    mEt.setText("");
                    mEt.setFocusableInTouchMode(false);
                    mEt.setFocusable(false);
                }
                if (mTvItem3 != ctv) {
                    mTvItem3.setChecked(false);

                } else {
                    cintex = 2;
                    mEt.setText("");
                    mEt.setFocusableInTouchMode(false);
                    mEt.setFocusable(false);
                }
                if (mTvItem4 != ctv) {
                    mTvItem4.setChecked(false);

                } else {
                    cintex = 3;
                    mEt.setFocusableInTouchMode(true);
                    mEt.setFocusable(true);
                }

                if (ctv.isChecked()) {
                    ctv.setChecked(false);
                } else {
                    ctv.setChecked(true);
                }
            }
        });

    }

    private boolean isSelect(GridView mGridView) {
        boolean b = false;
        SparseBooleanArray selectedposition_red = mGridView.getCheckedItemPositions();
        for (int i = 0; i < list_mudi.size(); i++) {
            if (selectedposition_red.get(i)) {
                return true;
            }
        }
        return b;
    }

    public ArrayList<JSONObject> getData() {
        try {
            SparseBooleanArray selectedposition_red = mGridView.getCheckedItemPositions();
            ArrayList<JSONObject> list = new ArrayList<>();
            for (int i = 0; i < list_mudi.size(); i++) {
                boolean b1 = selectedposition_red.get(i);
                if (b1) {
                    JSONObject jo = new JSONObject();
                    jo.put("topicId", "1");
                    jo.put("topicAnswerId", (i + 1) + "");
                    jo.put("topicTitle", "投资目的（可多选）");
                    jo.put("topicAnswerDesc", list_mudi.get(i));
                    jo.put("supplementText", "");
                    list.add(jo);
                }
            }

            JSONObject jo2a = new JSONObject();
            jo2a.put("topicId", "25");
            jo2a.put("topicAnswerId", gh.getSelectIndex() + 87 + "");
            jo2a.put("topicTitle", "主要投资目的");
            jo2a.put("topicAnswerDesc", list2a.get(gh.getSelectIndex()));
            jo2a.put("supplementText", "");

            JSONObject jo2 = new JSONObject();
            jo2.put("topicId", "2");
            jo2.put("topicAnswerId", mGridView2.getCheckedItemPosition() + 9 + "");
            jo2.put("topicTitle", "预投金额（RMB）");
            jo2.put("topicAnswerDesc", list_money.get(mGridView2.getCheckedItemPosition()));
            jo2.put("supplementText", "");

            JSONObject jo3 = new JSONObject();
            jo3.put("topicId", "3");
            jo3.put("topicAnswerId", cintex + 14 + "");
            jo3.put("topicTitle", "首选投资类型");

            jo3.put("supplementText", "");
            if (cintex == 0) {
                jo3.put("topicAnswerDesc", "定期存款或储蓄账户");
            } else if (cintex == 1) {
                jo3.put("topicAnswerDesc", "分散投资（股票、基金、债券等）");
            } else if (cintex == 2) {
                jo3.put("topicAnswerDesc", "投机类");
            } else {
                jo3.put("topicAnswerDesc", "");
                String s1 = mEt.getText().toString();
                jo3.put("supplementText", "其他（" + s1 + "）");
            }


            JSONObject jo4 = new JSONObject();
            jo4.put("topicId", "4");
            jo4.put("topicAnswerId", mGridView4.getCheckedItemPosition() + 18 + "");
            jo4.put("topicTitle", "投资期限");
            jo4.put("topicAnswerDesc", list_qixian.get(mGridView4.getCheckedItemPosition()));
            jo4.put("supplementText", "");


            JSONObject jo5 = new JSONObject();
            jo5.put("topicId", "5");
            jo5.put("topicAnswerId", mGridView5.getCheckedItemPosition() + 22 + "");
            jo5.put("topicTitle", "您认为需要多长时间才能达到您预期的投资回报？");
            jo5.put("topicAnswerDesc", list_time.get(mGridView5.getCheckedItemPosition()));
            jo5.put("supplementText", "");
            list.add(jo2a);
            list.add(jo2);
            list.add(jo3);
            list.add(jo4);
            list.add(jo5);
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvNext.setOnClickListener(this);

    }

    ArrayList<String> list2a;

    ArrayList<String> list_mudi;
    ArrayList<String> list_money;
    ArrayList<String> list_qixian;
    ArrayList<String> list_time;

    private void setAdapter() {
        list_mudi = new ArrayList<>();
        list_mudi.add("退休");
        list_mudi.add("保本");
        list_mudi.add("收入");
        list_mudi.add("对冲");
        list_mudi.add("抵制通胀");
        list_mudi.add("资本增值");
        list_mudi.add("套利");
        list_mudi.add("投机");
        mGridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        mAdapter1 = new ResearchSelectAdapter(getActivity(), list_mudi);
        mAdapter1.setMore(true);
        mGridView.setAdapter(mAdapter1);

        list2a = new ArrayList<>();
        list2a.add("保本");
        list2a.add("稳定收入");
        list2a.add("资本增值");
        list2a.add("稳定收入和资本增值均衡");


        list_money = new ArrayList<>();
        list_money.add("<50万");
        list_money.add("50~100万");
        list_money.add("100~500万");
        list_money.add("500~1000万");
        list_money.add(">1000万");

        mGridView2.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mAdapter2 = new ResearchSelectAdapter(getActivity(), list_money);
        mGridView2.setAdapter(mAdapter2);


        list_qixian = new ArrayList<>();
        list_qixian.add("短期（少于一年）");
        list_qixian.add("中短期（1~5年）");
        list_qixian.add("中期（6~10年）");
        list_qixian.add("长期（10年以上）");


        mGridView4.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mAdapter4 = new ResearchSelectAdapter(getActivity(), list_qixian);
        mGridView4.setAdapter(mAdapter4);

        list_time = new ArrayList<>();
        list_time.add(">10年");
        list_time.add("6~10年");
        list_time.add("1~5年");
        list_time.add("<1年");


        mGridView5.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mAdapter5 = new ResearchSelectAdapter(getActivity(), list_time);
        mGridView5.setAdapter(mAdapter5);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_next:
                RiskStateActivity fa = (RiskStateActivity) getActivity();

                String msg = checkSelect();
                if (msg != null) {

                    fa.showToast(msg);
                    return;
                }

                mOnNextClickListener.onNextClick();
                break;
            default:
                break;
        }
    }

    /**
     * 返回null 全部填写 否则返回错误提示
     *
     * @return
     */
    private String checkSelect() {
        String s1 = "您未回答问题:";
        if (!isSelect(mGridView)) {

            return s1 + quest.get(0);
        } else if (gh.getSelectIndex() == -1) {
            return s1 + quest.get(1);
        } else if (mGridView2.getCheckedItemPosition() == -1) {
            return s1 + quest.get(2);
        } else if (cintex == -1) {
            return s1 + quest.get(3);
        } else if (cintex == 3) {
            if (mEt.getText().toString().equals("")) {
                return "您未填写" + quest.get(3);
            }
        } else if (mGridView4.getCheckedItemPosition() == -1) {
            return s1 + quest.get(4);
        } else if (mGridView5.getCheckedItemPosition() == -1) {
            return s1 + quest.get(5);
        }
        return null;
    }
}
